package weather;

import com.alibaba.fastjson.JSONObject;
import com.show.api.ShowapiRequest;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by sirius_swu on 2017/3/13.
 */
public class QueryWeather {
    JSONObject jsonObject;

    public QueryWeather(String city) {
        ShowapiRequest req = new ShowapiRequest("http://ali-weather.showapi.com/area-to-weather","68ff8fb05e4e4952b162405a4ed97f0a");
        byte b[] = req.addTextPara("area",city).addTextPara("need3HourForcast", "1").addTextPara("needAlarm", "1").addTextPara("needHourData", "1").addTextPara("needIndex", "1").addTextPara("needMoreDay", "0").getAsByte();
        //打印返回头
        Map map = req.getRes_headMap();
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            Object k = it.next();
            System.out.println(k + "          " + map.get(k));
        }
        //打印返回体
        String	res = null;
        try {
            res = new String(b,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(res);
        //下面是使用阿里的fastjson包解析。如果不需要，请使用自己的解析包
        jsonObject= JSONObject.parseObject(res);
        // 03-14 08:07:47.091 18339-18374/? I/System.out: {"showapi_res_code":0,"showapi_res_error":"","showapi_res_body":{"time":"20170314000000","ret_code":0,"alarmList":[],"cityInfo":{"c6":"zhejiang","c5":"杭州","c4":"hangzhou","c3":"杭州","c9":"中国","c8":"china","c7":"浙江","c17":"+8","c16":"AZ9571","c1":"101210101","c2":"hangzhou","longitude":120.165,"c11":"0571","latitude":30.319,"c10":"1","c12":"310000","c15":"43"},"now":{"weather_code":"00","aqiDetail":{"co":0.745,"so2":9,"area":"杭州","o3":43,"no2":29,"area_code":"hangzhou","quality":"良","aqi":64,"pm10":67,"pm2_5":45,"o3_8h":51,"primary_pollutant":"颗粒物(PM2.5)"},"wind_direction":"北风","temperature_time":"07:59","wind_power":"3级","aqi":64,"sd":"80%","weather_pic":"http://app1.showapi.com/weather/icon/day/00.png","weather":"晴","temperature":"4"},"hourDataList":[{"weather_code":"301","aqiDetail":{"co":0.664,"so2":8,"area":"杭州","o3":61,"no2":27,"area_code":"hangzhou","quality":"优","aqi":35,"pm10":32,"pm2_5":23,"o3_8h":68,"primary_pollutant":""},"wind_direction":"东北风","temperature_time":"00:03","wind_power":"2级","aqi":35,"sd":"94%","weather_pic":"http://app1.showapi.com/weather/icon/day/301.png","weather":"雨","temperature":"9"},{"aqiDetail":{"co":0.664,"so2":8,"area":"杭州","o3":61,"no2":27,"area_code":"hangzhou","quality":"优","aqi":35,"pm10":32,"pm2_5":23,"o3_8h":68,"primary_pollutant":""},"weather_code":"01","wind_direction":"西北风","temperature_time":"00:44","wind_power":"2级","aqi":35,"sd":"89%","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","weather":"多云","temperature":"5"},{"aqiDetail":{"co":0.673,"so2":7,"area":"杭州","o3":54,"no2":31,"area_code":"hangzhou","quality":"优","aqi":38,"pm10":37,"pm2_5":25,"o3_8h":65,"primary_pollutant":""},"weather_code":"01","wind_direction":"西北风","temperature_time":"01:03","wind_power":"2级","aqi":38,"sd":"88%","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","weather":"多云","temperature":"5"},{"aqiDetail":{"co":0.673,"so2":7,"area":"杭州","o3":54,"no2":31,"area_code":"hangzhou","quality":"优","aqi":38,"pm10":37,"pm2_5":25,"o3_8h":65,"primary_pollutant":""},"weather_code":"01","wind_direction":"北风","temperature_time":"01:42","wind_power":"1级","aqi":38,"sd":"87%","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","weather":"多云","temperature":"5"},{"aqiDetail":{"co":0.7,"so2":7,"area":"杭州","o3":49,"no2":33,"area_code":"hangzhou","quality":"优","aqi":39,"pm10":38,"pm2_5":26,"o3_8h":49,"primary_pollutant":""},"weather_code":"01","wind_direction":"西北风","temperature_time":"02:02","wind_power":"2级","aqi":39,"sd":"86%","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","weather":"多云","temperature":"5"},{"aqiDetail":{"co":0.7,"so2":7,"area":"杭州","o3":49,"no2":33,"area_code":"hangzhou","quality":"优","aqi":39,"pm10":38,"pm2_5":26,"o3_8h":49,"primary_pollutant":""},"weather_code":"01","wind_direction":"西北风","temperature_time":"02:36","wind_power":"2级","aqi":39,"sd":"84%","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","weather":"多云","temperature":"5"},{"aqiDetail":{"co":0.682,"so2":7,"area":"杭州","o3":52,"no2":28,"area_code":"hangzhou","quality":"优","aqi":39,"pm10":38,"pm2_5":27,"o3_8h":50,"primary_pollutant":""},"weather_code":"01","wind_direction":"北风","temperature_time":"03:01","wind_power":"3级","aqi":39,"sd":"83%","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","weather":"多云","temperature":"5"},{"aqiDetail":{"co":0.682,"so2":7,"area":"杭州","o3":52,"no2":28,"area_code":"hangzhou","quality":"优","aqi":39,"pm10":38,"pm2_5":27,"o3_8h":50,"primary_pollutant":""},"weather_code":"01","wind_direction":"北风","temperature_time":"03:35","wind_power":"2级","aqi":39,"sd":"83%","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","weather":"多云","temperature":"5"},{"aqiDetail":{"co":0.718,"so2":8,"area":"杭州","o3":56,"no2":25,"area_code":"hangzhou","quality":"优","aqi":49,"pm10":48,"pm2_5":34,"o3_8h":52,"primary_polluta
        // 03-14 08:07:47.129 18339-18374/? I/System.out: {"ret_code":0,"f1":{"night_weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","jiangshui":"18%","day_weather":"多云","air_press":"1020 hPa","night_weather":"多云","night_air_temperature":"3","day_weather_code":"01","night_wind_direction":"北风","day_weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","day":"20170314","day_air_temperature":"12","day_wind_power":"3-4级 5.5~7.9m/s","night_wind_power":"3-4级 5.5~7.9m/s","night_weather_code":"01","3hourForcast":[{"temperature":"6","temperature_min":"5","hour":"8时-11时","weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","wind_power":"4-5级,2","temperature_max":"9","weather":"多云","wind_direction":"东北风"},{"temperature":"9","temperature_min":"6","hour":"11时-14时","weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","wind_power":"微风,2","temperature_max":"11","weather":"多云","wind_direction":"北风"},{"temperature":"11","temperature_min":"9","hour":"14时-17时","weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","wind_power":"3-4级,2","temperature_max":"11","weather":"多云","wind_direction":"北风"},{"temperature":"11","temperature_min":"9","hour":"17时-20时","weather_pic":"http://app1.showapi.com/weather/icon/day/01.png","wind_power":"微风,2","temperature_max":"11","weather":"多云","wind_direction":"北风"},{"temperature":"9","temperature_min":"7","hour":"20时-23时","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","wind_power":"微风,0","temperature_max":"11","weather":"多云","wind_direction":"北风"},{"temperature":"7","temperature_min":"4","hour":"23时-2时","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","wind_power":"3-4级,0","temperature_max":"9","weather":"多云","wind_direction":"北风"},{"temperature":"4","temperature_min":"3","hour":"2时-5时","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","wind_power":"微风,0","temperature_max":"7","weather":"多云","wind_direction":"北风"},{"temperature":"3","temperature_min":"3","hour":"5时-8时","weather_pic":"http://app1.showapi.com/weather/icon/night/01.png","wind_power":"微风,0","temperature_max":"6","weather":"多云","wind_direction":"北风"}],"ziwaixian":"最弱","sun_begin_end":"06:12|18:07","index":{"pk":{"title":"暂缺","desc":"暂缺"},"yh":{"title":"较适宜","desc":"不用担心天气来调皮捣乱而影响了兴致。 "},"xq":{"title":"较好","desc":"好天气会带来一天的好心情。"},"beauty":{"title":"滋润保湿","desc":"请选用滋润保湿型化妆品。"},"mf":{"title":"暂缺","desc":"暂缺"},"nl":{"title":"暂缺","desc":"暂缺"},"clothes":{"title":"较冷","desc":"建议着厚外套加毛衣等服装。"},"wash_car":{"title":"适宜","desc":"无雨且风力较小，易保持清洁度。"},"gj":{"title":"较适宜","desc":"风稍大，出门逛街前记得给秀发定型。"},"dy":{"title":"不适宜钓鱼","desc":"天气不好，不适合垂钓"},"uv":{"title":"最弱","desc":"辐射弱，涂擦SPF8-12防晒护肤品。"},"sports":{"title":"较适宜","desc":"温较低，推荐您进行室内运动。"},"cl":{"title":"暂缺","desc":"暂缺"},"ag":{"title":"暂缺","desc":"暂缺"},"hc":{"title":"较适宜","desc":"风大，需注意及时添衣。"},"zs":{"title":"不容易中暑","desc":"气温不高，中暑几率极低。"},"cold":{"title":"少发","desc":"无明显降温，感冒机率较低。"},"pj":{"title":"暂缺","desc":"暂缺"},"comfort":{"title":"较好","desc":"普遍感觉舒适"},"ac":{"title":"暂缺","desc":"暂缺"},"aqi":{"title":"良","desc":"气象条件有利于空气污染物扩散。"},"glass":{"title":"暂缺","desc":"暂缺"},"ls":{"title":"不适宜","desc":"天气阴沉，不太适宜晾晒。"},"travel":{"title":"较适宜","desc":"较弱降水和微风将伴您共赴旅程。"}},"weekday":2,"day_wind_direction":"北风"},"hourDataList":[{"temperature":"9","temperature_time":"00:03","sd":"94%","weather_pic":"http://app1.showapi.com/weather/icon/day/301.png","
        System.out.println(jsonObject.getJSONObject("showapi_res_body"));
    }

    /*
    * 返回Json
    * */
    public JSONObject retJson() {
        return jsonObject;
    }
}
